/**   
* @Description: TODO(用一句话描述该文件做什么) 
* @author thomas_liu 
* @date 2017年5月8日 下午11:45:16 
* @version V1.0   
*/
package com.thomas.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.thomas.entity.Blog;
import com.thomas.entity.PageBean;
import com.thomas.service.BlogService;
import com.thomas.util.PageUtil;
import com.thomas.util.StringUtil;

/** 
 * @ClassName: IndexContrller 
 * @Description: 主页Controller 
 * @author thomas_liu  
 * @date 2017年5月8日 下午11:45:16 
 *  
 */
@Controller
@RequestMapping("/")
public class IndexContrller {
	
	@Autowired
	private BlogService blogService;
	
	@RequestMapping("/index")
	public ModelAndView index(@RequestParam(value="page",required=false) String page,@RequestParam(value="releaseDateStr",required=false) String releaseDateStr,@RequestParam(value="typeId",required=false) String typeId,HttpServletRequest request) throws Exception{
		
		ModelAndView mav = new ModelAndView();
		if(StringUtil.isEmpty(page)){
			page = "1";
		}
	
		PageBean pagebean = new PageBean(Integer.parseInt(page),10);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("start", pagebean.getStart());
		map.put("size", pagebean.getPageSize());
		map.put("releaseDateStr", releaseDateStr);
		map.put("typeId", typeId);
		List<Blog> blogList = blogService.list(map);
		for(Blog blog:blogList){
			List<String> imageList = blog.getImageList();
			String blogInfo = blog.getContent();
			Document doc = Jsoup.parse(blogInfo);
			Elements jpgs = doc.select("img[src$=.jpg]");
			for(int i=0;i<jpgs.size();i++){
				Element jpg = jpgs.get(i);
				imageList.add(jpg.toString());
				if(i==2){
					break;
				}
			}
		}
		mav.addObject("blogList",blogList);
		StringBuffer param = new StringBuffer();
		if(StringUtil.isNotEmpty(typeId)){
			param.append("typeId="+typeId+"&");
		}
		if(StringUtil.isNotEmpty(releaseDateStr)){
			param.append("releaseDateStr="+releaseDateStr+"&");
		}
		mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index.html", blogService.getTotal(map), Integer.parseInt(page), 10, param.toString()));
		mav.addObject("pageTitle", "托马斯的半亩地");
		mav.addObject("mainPage", "foreground/blog/list.jsp");
		mav.setViewName("mainTemp");
		return mav;
		
	}
	
	/**
	 *源码下载 
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/download")
	public ModelAndView download() throws Exception{
		ModelAndView mav = new ModelAndView();
		mav.addObject("pageTitle", "本站源码下载页面_java开源博客系统");
		mav.addObject("mainPage", "foreground/system/download.jsp");
		mav.setViewName("mainTemp");
		return mav;
	}
}
